/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.common.generator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 

/**
 * The implementation of {@link ILanguageValidator} for the Java Programming language.
 *
 * @author Thomas Kistel
 * @version $Revision: 758 $
 */
public class JavaLanguageValidator implements ILanguageValidator {
    
    /**
     * An array with the Java language keywords.
     */
    public static final String[] JAVA_KEYWORDS = { "abstract", "assert",
        "boolean", "break", "byte", "case", "catch", "char", "class",
        "const", "continue", "default", "do", "double", "else", "enum",
        "extends", "false", "final", "finally", "float", "for", "goto", "if",
        "implements", "import", "instanceof", "int", "interface", "long",
        "native", "new", "null", "package", "private", "protected", "public",
        "return", "short", "static", "strictfp", "super", "switch",
        "synchronized", "this", "throw", "throws", "transient", "true" , "try",
        "void", "volatile", "while" };
    
    public static final String PACKAGE_NAME_REGEX = "([a-zA-Z_][a-zA-Z0-9_]*\\.)*[a-zA-Z_][a-zA-Z0-9_]*";
    public static final String JAVA_CLASS_NAME_SUFFIX_REGEX = "[a-zA-Z0-9_]*";
    public static final String JAVA_CLASS_NAME_PREFIX = "[A-Z_]";
    public static final String JAVA_FIELD_NAME_REGEX = "[a-z_][a-zA-Z0-9_]*";
    
    
    private static final Pattern PACKAGE_NAME = Pattern.compile(PACKAGE_NAME_REGEX);
    private static final Pattern JAVA_CLASS_NAME_SUFFIX = Pattern.compile(JAVA_CLASS_NAME_SUFFIX_REGEX);
    private static final Pattern JAVA_CLASS_NAME = Pattern.compile(JAVA_CLASS_NAME_PREFIX + JAVA_CLASS_NAME_SUFFIX);
    private static final Pattern JAVA_FIELD_NAME = Pattern.compile(JAVA_FIELD_NAME_REGEX);
    
    

    /* (non-Javadoc)
     * @see de.thwildau.tm.moses.common.validator.ILanguageValidator#isKeyword(java.lang.String)
     */
    @Override
    public boolean isKeyword(String value) {
        for(String keyword : JAVA_KEYWORDS) {
            Pattern pattern = Pattern.compile("(?:^|\\.)" + keyword + "(?:$|\\.)");
            Matcher matcher = pattern.matcher(value);
            while (matcher.find()) {
                return true;
            }
        }
        return false;
    }

    /* (non-Javadoc)
     * @see de.thwildau.tm.moses.common.validator.ILanguageValidator#validPackageName(java.lang.String)
     */
    @Override
    public boolean validPackageName(String value) {
        return PACKAGE_NAME.matcher(value).matches();
    }

    /* (non-Javadoc)
     * @see de.thwildau.tm.moses.common.validator.ILanguageValidator#validClassName(java.lang.String)
     */
    @Override
    public boolean validClassName(String value) {
        return JAVA_CLASS_NAME.matcher(value).matches();
    }

    @Override
    public boolean validClassNameSuffix(String value) {
        return JAVA_CLASS_NAME_SUFFIX.matcher(value).matches();
    }

    @Override
    public boolean validFieldName(String value) {
        return JAVA_FIELD_NAME.matcher(value).matches();
    }
}
